<?php

namespace app\api\service\wanlshop;

use think\Model;
use addons\wanlshop\library\WanlSdk\Common;

class GoodsSku extends Model
{

    // 表名
    protected $name = 'wanlshop_goods_sku';

    /**
     * @param $sku_id
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * 库存入缓存
     */
	public function addStockToCache($sku_id)
    {
        $redis = Common::redis();
        $sku = $this->get($sku_id);
        $sku_key = 'goods_'.$sku['goods_id'].'_'.$sku['id'];
        // 获取缓存数量
        $llen = $redis->llen("{$sku_key}");
        if(!$llen){
            for ($i = 0; $i < $sku['stock']; $i ++) {
                $redis->lpush("{$sku_key}", 1);
            }
        }
        return $redis->llen("{$sku_key}");
    }
}
